package com.gxa.autoparts.portal.controller;
import com.gxa.autoparts.core.common.JsonResult;

import com.gxa.autoparts.core.pojo.Address;
import com.gxa.autoparts.portal.service.AddressManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/userAddressManagerController")
@Api(tags = "用户地址管理")
public class UserAddressManagerController {

    @Autowired
    AddressManagerService addressManagerService=null;

    @PostMapping("/findAddress")
    @ApiOperation("查询用户所有地址")
    public Map findAll(Long id){
        //这为了测试方便，添加用户id，实际实现使用threadlocal
        List<Address> address = addressManagerService.findAddress(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(address);
        return jsonResult.getDataMap();

    }

    @PostMapping("/findOne")
    @ApiOperation("查询用户单个地址")
    public Map findOne(Long id){
        //这为了测试方便，添加用户id，实际实现使用threadlocal
        Address one = addressManagerService.findOne(id);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(one);
        return jsonResult.getDataMap();

    }
    @PostMapping("/updateAddress")
    @ApiOperation("修改用户所有地址")
    public Map update(@RequestBody Address address){
        addressManagerService.updateAddress(address);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();

    }
    @PostMapping("/deleteAddress")
    @ApiOperation("删除用户地址")
    public Map delete(@RequestParam("id") Long id){
        addressManagerService.deleteById(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();

    }

}
